package com.lyun.controller;

import com.lyun.bo.GetAllMovementBo;
import com.lyun.common.*;
import com.lyun.dto.MovementsDto;
import com.lyun.pojo.Movements;
import com.lyun.service.*;
import com.lyun.vo.MovementsVo;
import com.lyun.vo.PageVO;
import com.lyun.vo.VisitorsVo;
import io.jsonwebtoken.Claims;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.data.geo.Point;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/movements")
public class MovementsController {

    @Resource
    private VisitorService visitorService;

    @DubboReference
    private UserService userService;

    @DubboReference
    private LoveValueService loveValueService;

    @Resource
    private MovementsService movementsService;

    @Resource
    private OssTemplate ossTemplate;

    @Resource
    private RedisTemplate<Object, Object> redisTemplate;

    @DubboReference
    private CommentService commentService;

    @Resource
    private MovementLikeService movementLikeService;

    @Resource
    private RedisGeoUtils redisGeoUtils;

    /**
     * 我的访客列表
     *
     * @param token token
     * @return List<VisitorsVo>
     */
    @GetMapping("/visitors")
    public List<VisitorsVo> getVisitors(@RequestHeader("Authorization") String token) {
        Claims claims = JwtUtils.getClaims(token);
        Long id = ((Integer) claims.get("id")).longValue();

        return visitorService.selectVisitorsListById(id);
    }

    /**
     * 14-APP-发布圈子动态
     *
     * @throws IOException
     */
    @PostMapping
    public R<String> publishMovement(MovementsDto dto) throws IOException {

        Long id = BaseContext.getCurrentId();

        Movements movements = new Movements();
        movements.setTextContent(dto.getTextContent());
        movements.setLatitude(Double.valueOf(dto.getLatitude()));
        movements.setLongitude(Double.valueOf(dto.getLongitude()));
        movements.setLocation(dto.getLocation());
        movements.setCreateTime(LocalDateTime.now());
        movements.setUserId(id);
        movements.setStatus(0);

        List<MultipartFile> fileList = dto.getImageContent();

        if (!CollectionUtils.isEmpty(fileList)) {
            ArrayList<String> imageList = new ArrayList<>();
            for (MultipartFile multipartFile : fileList) {
                String upload = ossTemplate.upload(multipartFile);
                String s = upload.split("//")[2];
                imageList.add(s);
            }
            //movements.setImageContent(imageList.toString().replace("[", "").replace("]", "").trim());
            //去掉首尾两个字符 [ ] 再用.trim方法将字符串两端的空格去掉。
            String trim = imageList.toString().substring(1, imageList.toString().length() - 1).trim();

            movements.setImageContent(trim);
        }

        //改为管理端审核通过了再加到redisGeo
//        Long movementId = movementsService.saveAndGetId(movements);
        //redisTemplate.opsForGeo().add(EveryConstant.REDIS_GEO_MOVEMENTS, new Point(Double.parseDouble(dto.getLongitude()), Double.parseDouble(dto.getLatitude())), movementId.toString());

        return R.success("发布成功");
    }

    @GetMapping("all")
    public PageVO<MovementsVo> getAllMovement(@RequestParam(value = "page", required = false) Integer page,
                                              @RequestParam(value = "pagesize", required = false) Integer pagesize,
                                              @RequestParam(value = "userId", required = false) Long userId) {
        Long id = BaseContext.getCurrentId();
        GetAllMovementBo bo = new GetAllMovementBo();
        bo.setUserId(id.equals(userId) ? id : userId);
        bo.setPage(page);
        bo.setPageSize(pagesize);

        return movementsService.selectMovenmentsPageByUserId(bo);
    }

    /**
     * 19-APP-查询推荐动态
     * 优先推荐自己周围五公里的动态
     *
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping("/recommend")
    public PageVO<MovementsVo> getFriendMovements(@RequestParam(value = "page", required = false) Integer page,
                                                  @RequestParam(value = "pagesize", required = false) Integer pagesize) {

        Long id = BaseContext.getCurrentId();

        return movementsService.getRecommendFriendList(id, page, pagesize);

    }

    /**
     * 20-APP-查询单条动态
     *
     * @param movementId
     * @return
     */
    @GetMapping("/{id}")
    public MovementsVo getMovements(@PathVariable("id") Long movementId) {

        Long id = BaseContext.getCurrentId();

        return movementsService.getMovementInfoByMovementId(movementId, id);
    }

    /**
     * 23-给动态点赞
     *
     * @param movementId
     */
    @GetMapping("/{id}/like")
    public Integer likeMovement(@PathVariable("id") Long movementId) {

        Long userId = BaseContext.getCurrentId();

        return movementLikeService.updateOrSave(userId, movementId);
    }

    /**
     * 24-APP-动态取消点赞
     *
     * @param movementId
     */
    @GetMapping("/{id}/dislike")
    public Integer dislikeMovement(@PathVariable("id") Long movementId) {

        Long userId = BaseContext.getCurrentId();

        return movementLikeService.deleteOrUpdate(userId, movementId);

    }
}
